package com.ysy.tz.mfg.equipment.rfid;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import purejavacomm.CommPortIdentifier;
import purejavacomm.SerialPort;
import purejavacomm.SerialPortEvent;
import purejavacomm.SerialPortEventListener;

public class Rfid_KW9036 implements SerialPortEventListener {
    private static final Logger log = LoggerFactory.getLogger(Rfid_KW9036.class);
    InputStream inputStream;
    OutputStream outputStream;
    String portName;
    RfidCallback callback;
    private String lastRfid = null;

    public Rfid_KW9036(String portName, RfidCallback callback) {
        this.portName = portName;
        this.callback = callback;
    }

    public void init() throws Exception {
        Enumeration portList = CommPortIdentifier.getPortIdentifiers();

        while(portList.hasMoreElements()) {
            CommPortIdentifier portId = (CommPortIdentifier)portList.nextElement();
            if (portId.getPortType() == 1 && portId.getName().equals(this.portName)) {
                this.simpleRun(portId);
                break;
            }
        }

    }

    public void simpleRun(CommPortIdentifier portId) throws Exception {
        SerialPort serialPort = (SerialPort)portId.open("SimpleReadApp", 1000);
        this.inputStream = serialPort.getInputStream();
        this.outputStream = serialPort.getOutputStream();
        serialPort.addEventListener(this);
        serialPort.notifyOnDataAvailable(true);
        serialPort.setSerialPortParams(19200, 8, 1, 0);
    }

    public void serialEvent(SerialPortEvent event) {
        if (event.getEventType() == 1) {
            try {
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                byte[] buf = new byte[64];
                boolean var4 = true;

                while(this.inputStream.available() > 0) {
                    int len = this.inputStream.read(buf);
                    if (len < 0) {
                        break;
                    }

                    baos.write(buf, 0, len);
                }

                byte[] data = baos.toByteArray();
                StringBuffer sb = new StringBuffer();

                for(int i = 0; i < data.length; ++i) {
                    String s = Integer.toHexString(data[i]);
                    s = s.replaceFirst("ffffff", "");
                    sb.append(s);
                }

                String rs = sb.toString();

                try {
                    this.callback.receiveRfid(rs);
                } catch (Exception var9) {
                    log.error("rfid业务处理错误", var9);
                }
            } catch (Exception var10) {
                log.error("rfid解析错误", var10);
            }
        }

    }

    public static void main(String[] args) throws Exception {
        Rfid_KW9036 reader = new Rfid_KW9036("COM9", new RfidCallback() {
            public void receiveRfid(String rfid) throws Exception {
                System.out.println("rfid: " + rfid);
            }

            public void receiveEmpty() throws Exception {
            }
        });
        reader.init();
    }
}
